package com.gwt.client.login;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;


public class LogIn  implements ActionListener{

	private JFrame frame;
	private JTextField userName;
	private JPasswordField passwordField;
	private JFrame controllingFrame;
	private static String logIn ="logIn";   
    private static String HELP = "help";
    private static String logOut = "logOut";
    private static String user;
    private static int cont = 0;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LogIn window = new LogIn();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public LogIn() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		userName = new JTextField();
		userName.setBounds(50, 30, 100, 20);
		frame.getContentPane().add(userName);
		userName.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(167, 30, 100, 20);
		passwordField.setActionCommand(logIn);
        passwordField.addActionListener(this);
		frame.getContentPane().add(passwordField);
		
		JLabel label = new JLabel("username");
		label.setBounds(65,57, 110, 14);
		frame.getContentPane().add(label);
		
		JLabel label_1 = new JLabel("Password ");
		label_1.setBounds(185, 57, 110, 14);
		frame.getContentPane().add(label_1);
		
		JButton logInButton = new JButton("Log In");
		logInButton.setBounds(317, 30, 89, 23);
		frame.getContentPane().add(logInButton);
		
		JButton helpButton = new JButton("Help");
		helpButton.setBounds(317, 64, 89, 23);
		frame.getContentPane().add(helpButton);
		
		JButton logOutButton = new JButton("log out");
		logOutButton.setBounds(317, 98, 89, 23);
		frame.getContentPane().add(logOutButton);
	
	
       
        //JButton SignOffButton = new JButton("Sign Out");
        logOutButton.setActionCommand(logOut);
        logInButton.setActionCommand(logIn);
        helpButton.setActionCommand(HELP);
        logInButton.addActionListener(this);
        helpButton.addActionListener(this);
        logOutButton.addActionListener(this);
	}
	
	
    public void actionPerformed(ActionEvent e) {
	    
    	
        String cmd = e.getActionCommand();
   	
       /*checking username*/
        if (logIn.equals(cmd)) { //Process the password.this is the piece of program that does the checking of Password
        	 user = userName.getText();
        	 //char[] input = passwordField.getPassword();
        	 if (isUserCorrect(user))
        	 {
        		 	
        		 
        		 	//resetFocus2();
	           	 
		            char[] input = passwordField.getPassword();
		            if (isPasswordCorrect(input))
		            {
		            	JOptionPane.showMessageDialog(controllingFrame,
		                  "Success! You are now entering into the system.");
		            	passwordField.setText("");
		            	resetFocus1();		                
		                userName.setText("");
		            }
		            
		          
		            else 
		            { if(cont <= 1){
		            	JOptionPane.showMessageDialog(controllingFrame,
		                "Invalid password. Try again.",
		                "Error Message",
		                 JOptionPane.ERROR_MESSAGE);
		            	cont ++;
		            	resetFocus2();
		            	passwordField.setText("");
		            }
		            		
		            	
		            	
		            else{
		            	
		                
		            	JOptionPane.showMessageDialog(controllingFrame,
		    	         "YOUR USER ID IS LOCKED" +
		    	          "     You've exceded the maximun number of logon attempts allowed.As a result, we've disabled your User ID and" +
		    	          "Password to protect your confidential information.Please look for personal assistance",
		    	          "Error Message",
		    	          JOptionPane.ERROR_MESSAGE);
		                  cont = 0;
		            		passwordField.setText("");
			            			                
			                userName.setText("");
		            		resetFocus1();}
		            		
		            	} 
		                   
		            
	                
        	 	}
        	 
        	 
        	 
        	 
        	 
        	 /*################# CASE IN WHICH USENAME IS INCORRECT#####*/
        	 
        	 else{
	               if(cont <= 1){
	            		JOptionPane.showMessageDialog(controllingFrame,
		                		"Invalid userName. Try again.",
			                    "Error Message",
			                    JOptionPane.ERROR_MESSAGE);
			                cont ++;
			                resetFocus1();
			                userName.selectAll();
			                passwordField.setText("");
			            	resetFocus1();		                
			                userName.setText("");
	               }
	            	   
	            	   
	               else{
	                	JOptionPane.showMessageDialog(controllingFrame,
	    	                    "YOUR USER ID IS LOCKED" +
	    	                    "     You've exceded the maximun number of logon attempts allowed.As a result, we've disabled your User ID and" +
	    	                    "Password to protect your confidential information.Please look for personal assistance",
	    	                    "Error Message",
	    	                    JOptionPane.ERROR_MESSAGE);
	                	cont = 0;
	                	
	                	passwordField.setText("");
		            	resetFocus1();		                
		                userName.setText("");
	               
	               
	               }     	 
        	 
        	 
        	 		} 
        }
        else if (HELP.equals(cmd)) {
        
         //The user has asked for help.
    		JOptionPane.showMessageDialog(controllingFrame,
    		"Check your spelling and the capitalization; however if you can't remember"
    		+" your password or usename, please look for personal assistance,");

    		resetFocus1();
		}   	 
        else if (logOut.equals(cmd)){	 
        	  //The user has asked for help.
    		JOptionPane.showMessageDialog(controllingFrame,
    		"You're going out of this system now");

    		resetFocus1();
        }
    	
    }
    
    /**
 
     
     */
        private static boolean isUserCorrect(String user) {
	       
	        boolean isUserCorrect = false;
	        String correctUser =  "j"; // testing usename

	       
	        	
	        	if(correctUser.equals(user)){
	        	isUserCorrect = true;
	        	}

	    
	        return isUserCorrect;
	    }   
        
    private static boolean isPasswordCorrect(char[] input) {
        boolean isCorrect = true;
        char[] correctPassword = { 'j'}; // testing the password

        
            isCorrect = Arrays.equals (input, correctPassword);
        

        //Zero out the password.
        Arrays.fill(correctPassword,'0');

        return isCorrect;
    }


 protected void resetFocus1() {
	
		
        userName.requestFocusInWindow();
	  
    }
 protected void resetFocus2() {
		
		
	 passwordField.requestFocusInWindow();

	  
    }
  

}
